from django.shortcuts import render

# Create your views here.

from fdfs_client.client import Fdfs_client
from rest_framework import request
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination

# client = Fdfs_client('utils/fastdfs/client.conf')
# client.upload_by_filename('/home/python/Desktop/0.jpg')

from rest_framework.generics import ListAPIView
from goods.models import SKU
from .serializers import SKUSerializer

# Create your views here.

class HotSKUListView(ListAPIView):
    """
    获取热销商品
    GET /goods/categories/(?P<category_id>\d+)/hotskus/
    """
    serializer_class = SKUSerializer
    pagination_class = None  #不分页

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]


class SKUAPIView(ListAPIView):
    serializer_class = SKUSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time','price','sales')

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-price')[:2]

from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer